package guo.mybatis.result;

import javax.management.ReflectionException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.*;

public class SimpleObjectFactory implements Serializable {
	private static final long serialVersionUID = -2526545106771266312L;

	public <T> Object create(Class<T> type) {
		return this.create(type, (List)null, (List)null);
	}

	public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
		Class<?> classToCreate = this.resolveInterface(type);
		return (T) this.instantiateClass(classToCreate, constructorArgTypes, constructorArgs);
	}

	protected Class<?> resolveInterface(Class<?> type) {
		Class classToCreate;
		if (type != List.class && type != Collection.class && type != Iterable.class) {
			if (type == Map.class) {
				classToCreate = HashMap.class;
			} else if (type == SortedSet.class) {
				classToCreate = TreeSet.class;
			} else if (type == Set.class) {
				classToCreate = HashSet.class;
			} else {
				classToCreate = type;
			}
		} else {
			classToCreate = ArrayList.class;
		}

		return classToCreate;
	}

	<T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
		try {
			Constructor constructor;
			if (constructorArgTypes != null && constructorArgs != null) {
				constructor = type.getDeclaredConstructor((Class[])constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
				if (!constructor.isAccessible()) {
					constructor.setAccessible(true);
				}

				return (T) constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
			} else {
				constructor = type.getDeclaredConstructor();
				if (!constructor.isAccessible()) {
					constructor.setAccessible(true);
				}

				return (T) constructor.newInstance();
			}
		} catch (Exception var9) {
			StringBuilder argTypes = new StringBuilder();
			if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {
				Iterator var6 = constructorArgTypes.iterator();

				while(var6.hasNext()) {
					Class<?> argType = (Class)var6.next();
					argTypes.append(argType.getSimpleName());
					argTypes.append(",");
				}

				argTypes.deleteCharAt(argTypes.length() - 1);
			}

			StringBuilder argValues = new StringBuilder();
			if (constructorArgs != null && !constructorArgs.isEmpty()) {
				Iterator var11 = constructorArgs.iterator();

				while(var11.hasNext()) {
					Object argValue = var11.next();
					argValues.append(String.valueOf(argValue));
					argValues.append(",");
				}

				argValues.deleteCharAt(argValues.length() - 1);
			}

		}
		return null;
	}

	public <T> boolean isCollection(Class<T> aClass) {
		return false;
	}
}
